// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

/**
 *     手势 输入 
 */

import qrj_math from "./qrj_math";
import qrj_node from "./qrj_node";
import qrj_tools from "./qrj_tools";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Touch_gesture extends cc.Component {

    // onLoad () {}
    start() { }
    // update (dt) {}

    /**
     *    监听 双击 事件  ,  max_time最大间隔时间
     */
    public static on_double_chick(n: cc.Node, succ_event: Function, max_time = 500) {
        let bt = 0;
        qrj_node.set_node_touch_event(n, () => {
            if (bt == 0) {
                bt = qrj_tools.getCurrentTime();
            } else {
                if (qrj_tools.getCurrentTime() - bt < max_time) {
                    bt = 0;
                    succ_event && succ_event();
                } else {
                    bt = qrj_tools.getCurrentTime();
                }
            }
        })
    }

    /**
     *    监听 下滑 手势    min_size 为滑动距离 , max_time 为最大时间
     */
    public static on_swipe_down(n: cc.Node, succ_event: Function, min_size = 50, max_time = 2000) {
        let bt = 0;
        qrj_node.set_node_touch_event(n,
            (event: cc.Event.EventTouch) => {
                bt = qrj_tools.getCurrentTime()
            },
            (event: cc.Event.EventTouch) => {

            },
            (event: cc.Event.EventTouch) => {
                if (Math.abs(event.getLocationX() - event.getStartLocation().x) > Math.abs(event.getLocationY() - event.getStartLocation().y)) {
                    return;
                }
                if (qrj_tools.getCurrentTime() - bt > max_time) {
                    return;
                }
                if (Math.abs(event.getLocationY() - event.getStartLocation().y) < min_size) {
                    return;
                }
                if (event.getLocationY() > event.getStartLocation().y) {
                    return;
                }
                succ_event && succ_event();
            },
            (event: cc.Event.EventTouch) => {
                if (Math.abs(event.getLocationX() - event.getStartLocation().x) > Math.abs(event.getLocationY() - event.getStartLocation().y)) {
                    return;
                }
                if (qrj_tools.getCurrentTime() - bt > max_time) {
                    return;
                }
                if (Math.abs(event.getLocationY() - event.getStartLocation().y) < min_size) {
                    return;
                }
                if (event.getLocationY() > event.getStartLocation().y) {
                    return;
                }
                succ_event && succ_event();
            },
        );
    }

    /**
     *    监听 上滑 手势      min_size 最小滑动距离 max_time 最大时间
     */
    public static on_swipe_up(n: cc.Node, succ_event: Function, min_size = 50, max_time = 2000) {
        let bt = 0;
        qrj_node.set_node_touch_event(n,
            (event: cc.Event.EventTouch) => {
                bt = qrj_tools.getCurrentTime()
            },
            (event: cc.Event.EventTouch) => {

            },
            (event: cc.Event.EventTouch) => {
                if (Math.abs(event.getLocationX() - event.getStartLocation().x) > Math.abs(event.getLocationY() - event.getStartLocation().y)) {
                    return;
                }
                if (qrj_tools.getCurrentTime() - bt > max_time) {
                    return;
                }
                if (Math.abs(event.getLocationY() - event.getStartLocation().y) < min_size) {
                    return;
                }
                if (event.getLocationY() < event.getStartLocation().y) {
                    return;
                }
                succ_event && succ_event();
            },
            (event: cc.Event.EventTouch) => {
                if (Math.abs(event.getLocationX() - event.getStartLocation().x) > Math.abs(event.getLocationY() - event.getStartLocation().y)) {
                    return;
                }
                if (qrj_tools.getCurrentTime() - bt > max_time) {
                    return;
                }
                if (Math.abs(event.getLocationY() - event.getStartLocation().y) < min_size) {
                    return;
                }
                if (event.getLocationY() < event.getStartLocation().y) {
                    return;
                }
                succ_event && succ_event();
            },
        );
    }

    /**
     *    监听 左右划    min_size最小有效距离
     */
    public static on_swipe_leftRight(n: cc.Node, succ_event: Function, left_succ_event: Function = null, right_succ_event: Function = null, min_size = 50) {
        let min_x = 0;
        let max_x = 0;
        let state = 0; /// 0=未达到 1=左 2=右
        qrj_node.set_node_touch_event(n,
            (event: cc.Event.EventTouch) => {

            },
            (event: cc.Event.EventTouch) => {
                let t = event.getLocationX() - event.getStartLocation().x;
                if (t > max_x) {
                    max_x = t;
                } else if (t < min_x) {
                    min_x = t;
                }
                ////////////////////////// 左右滑动 过程事件
                switch (state) {
                    case 0:
                        if (event.getStartLocation().x - event.getLocationX() > min_size) {
                            state = 1;
                            left_succ_event && left_succ_event();
                        } else if (event.getLocationX() - event.getStartLocation().x > min_size) {
                            state = 2;
                            right_succ_event && right_succ_event();
                        }
                        break;
                    case 1:
                        if (event.getLocationX() - event.getStartLocation().x > min_size) {
                            state = 2;
                            right_succ_event && right_succ_event();
                        }
                        break;
                    case 2:
                        if (event.getStartLocation().x - event.getLocationX() > min_size) {
                            state = 1;
                            left_succ_event && left_succ_event();
                        }
                        break;
                }

            },
            (event: cc.Event.EventTouch) => {
                if (Math.abs(min_x) < min_size || Math.abs(max_x) < min_size) {
                    return;
                }
                succ_event && succ_event();
            },
            (event: cc.Event.EventTouch) => {
                if (Math.abs(min_x) < min_size || Math.abs(max_x) < min_size) {
                    return;
                }
                succ_event && succ_event();
            },
        );
    }


    /**
     *    监听 两只 触摸 放大缩小
     */
    public static on_swipe_scale(n: cc.Node, scale_event: (scale: number) => void) {
        qrj_node.set_node_touch_event(n,
            (e: cc.Event.EventTouch) => {

            },
            (e: cc.Event.EventTouch) => {
                let ts = e.getTouches();
                if (ts.length >= 2) {
                    let bjl = qrj_math.get_space_2_position(ts[0].getStartLocation(), ts[1].getStartLocation());
                    let cjl = qrj_math.get_space_2_position(ts[0].getLocation(), ts[1].getLocation());
                    scale_event(cjl / bjl - 1);
                }
            });
    }
}
